<?php

define('ABSTRACT_NOTIFICATION_PARENT', 'object');
define('ABSTRACT_NOTIFICATION_SUBTYPE', 'abstract_notification');
define('ABSTRACT_NOTIFICATION_CLASS', 'AbstractNotification');

define('ABSTRACT_NOTIFICATION_SENDER_ID', 'sender_id');
define('ABSTRACT_NOTIFICATION_RECEIVER_ID', 'receiver_id');
define('ABSTRACT_NOTIFICATION_REFERER_ID', 'referer_id');
define('ABSTRACT_NOTIFICATION_TIMESTAMP', 'timestamp');

define('ABSTRACT_NOTIFICATION_CLIENT_ID', 'client_id');

// Status
define('ABSTRACT_NOTIFICATION_STATUS', 'status');
define('ABSTRACT_NOTIFICATION_STATUS_NEW', 'new');
define('ABSTRACT_NOTIFICATION_STATUS_DONE', 'done');

// Response
define('ABSTRACT_NOTIFICATION_RESPONSE', 'response');
define('ABSTRACT_NOTIFICATION_RESPONSE_NEW', 'new');
define('ABSTRACT_NOTIFICATION_RESPONSE_YES', 'yes');
define('ABSTRACT_NOTIFICATION_RESPONSE_NO', 'no');
define('ABSTRACT_NOTIFICATION_RESPONSE_LATER', 'later');
// Response can be: YES/ACCEPT - NO/DECLINE - DECIDE LATER

define('ABSTRACT_NOTIFICATION_TYPE', 'type');
define('ABSTRACT_NOTIFICATION_TYPE_REQUEST', 'request');
define('ABSTRACT_NOTIFICATION_TYPE_RESPONSE', 'response');
define('ABSTRACT_NOTIFICATION_TYPE_CANCEL', 'cancel');

register_entity_type(ABSTRACT_NOTIFICATION_PARENT, ABSTRACT_NOTIFICATION_SUBTYPE);
// This operation only affects the db on the first call for this subtype
// If you change the class name, you'll have to hand-edit the db
add_subtype(ABSTRACT_NOTIFICATION_PARENT, ABSTRACT_NOTIFICATION_SUBTYPE, ABSTRACT_NOTIFICATION_CLASS);

//require_once(dirname(dirname(__FILE__)) . '/DondeObject.php');

class AbstractNotification extends ElggObject {

    protected function initialise_attributes() {
        parent::initialise_attributes ();
        $this->attributes ['subtype'] = ABSTRACT_NOTIFICATION_SUBTYPE;
        $this->access_id = ACCESS_PUBLIC;
    }

    public function __construct($guid = null) {
        parent::__construct($guid);
    }

    // more customizations here
    public function saveNotification($senderid, $receiverid, $refererid, $type, $response = ABSTRACT_NOTIFICATION_RESPONSE_NEW, $client_id) {
        $this->save();
        $this->setMetaData('greengar_notification', 1);
        $this->owner_guid = $receiverid;
        $this->setMetaData(ABSTRACT_NOTIFICATION_SENDER_ID, $senderid);
        $this->setMetaData(ABSTRACT_NOTIFICATION_RECEIVER_ID, $receiverid);
        $this->setMetaData(ABSTRACT_NOTIFICATION_REFERER_ID, $refererid ? $refererid : -1);
        $this->setMetaData(ABSTRACT_NOTIFICATION_TIMESTAMP, time());
        $this->setMetaData(ABSTRACT_NOTIFICATION_STATUS, ABSTRACT_NOTIFICATION_STATUS_NEW);
        $this->setMetaData(ABSTRACT_NOTIFICATION_RESPONSE, $response);
        $this->setMetaData(ABSTRACT_NOTIFICATION_TYPE, $type);
        $this->setMetaData(ABSTRACT_NOTIFICATION_CLIENT_ID, $client_id);
        return $this->save();
    }

    public function responseNotification($response) {
        $this->save();
        $this->setMetaData(ABSTRACT_NOTIFICATION_STATUS, ABSTRACT_NOTIFICATION_STATUS_DONE);
        $this->setMetaData(ABSTRACT_NOTIFICATION_RESPONSE, $response);
        return $this->save();
    }

    public function toArray() {
        $sender = get_entity($this->sender_id);
        $receiver = get_entity($this->receiver_id);
        $referer = get_entity($this->referer_id);
        return array(
            'guid' => $this->getGUID(),
            'sender' => $sender ? userToArray($sender, $this->client_id, FALSE) : NULL,
            'refererid' => $this->referer_id,
            'response' => $this->response,
            'status' => $this->status,
            'time' => $this->getTimeCreated()
        );
    }

}

?>